package com.xgkj.diyiketang.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.sina.weibo.sdk.api.share.BaseResponse;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.constant.WBConstants;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.R;

public class WbcallbackActivity extends Activity implements IWeiboHandler.Response{
    public static final String WEIBO_SHARE_SUCCESS_ACTION = "weibo_share_success_action";
    private IWeiboShareAPI mWeiboShareAPI = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PalLog.e("微播回调", "on create");
        mWeiboShareAPI = WeiboShareSDK.createWeiboAPI(this,
                ProtocolConst.WEIBO_APP_KEY);//这粒要换
        mWeiboShareAPI.registerApp();
        mWeiboShareAPI.handleWeiboResponse(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mWeiboShareAPI.handleWeiboResponse(intent,this);
    }

    @Override
    public void onResponse(BaseResponse baseResponse) {
        PalLog.e("微博回调",baseResponse.errCode+"********"+baseResponse.errMsg);
        switch (baseResponse.errCode) {
            case WBConstants.ErrorCode.ERR_OK:
                Toast.makeText(this, R.string.weibosdk_toast_share_success,
                        Toast.LENGTH_LONG).show();
                //doShareWeiboGetPoint();
                break;
            case WBConstants.ErrorCode.ERR_CANCEL:
                Toast.makeText(this, R.string.weibosdk_toast_share_canceled,
                        Toast.LENGTH_LONG).show();
                break;
            case WBConstants.ErrorCode.ERR_FAIL:
                Toast.makeText(
                        this,
                        getString(R.string.weibosdk_toast_share_failed)
                                + "Error Message: " + baseResponse.errMsg,
                        Toast.LENGTH_LONG).show();
                break;
        }
        PalLog.e("微博回调",baseResponse.errCode+"****#######****"+baseResponse.errMsg);

        finish();
        if (baseResponse.errCode == WBConstants.ErrorCode.ERR_OK){
            Intent intent = new Intent(WEIBO_SHARE_SUCCESS_ACTION);
            sendBroadcast(intent);
        }

    }
}
